import openpyxl
import shutil
import os
import re

def process_file(excel_filename, target_dir):
    wb = openpyxl.load_workbook(excel_filename)
    ws = wb.active

    for row in range(2, ws.max_row + 1):
        # 读取每行数据
        filename = ws.cell(row=row, column=1).value
        original_name = ws.cell(row=row, column=2).value
        original_group = ws.cell(row=row, column=3).value
        new_name = ws.cell(row=row, column=4).value
        new_group = ws.cell(row=row, column=5).value

        #新名称为空，不做处理
        if not new_name:
            continue

        # 检查是否需要跳出循环
        if original_name == new_name and original_group == new_group:
            continue

        # 默认修改分组 特殊处理
        if original_name == new_name and original_group == '' and not new_group:
            continue

        # 创建新分组文件夹
        target_dir = os.path.expanduser(target_dir)
        if new_group:
            new_group_dir = os.path.join(target_dir, new_group)
        else:
            new_group_dir = os.path.join(target_dir, "默认修改分组")
        os.makedirs(new_group_dir, exist_ok=True)

        if not new_group:
            new_group="默认修改分组";

        # 复制文件并修改名称
        if new_name:
            new_filename = str(new_name)+ ".xmp"
        else:
            new_filename = str(original_name)+ ".xmp"
        new_filepath = os.path.join(new_group_dir, new_filename)
        if os.path.exists(new_filepath):
            # 文件已存在，添加自然数后缀
            new_filename, ext = os.path.splitext(new_filename)
            i = 1
            while os.path.exists(os.path.join(new_group_dir, f"{new_filename}_{i}{ext}")):
                i += 1
            new_filepath = os.path.join(new_group_dir, f"{new_filename}_{i}{ext}")
        shutil.copyfile(filename, new_filepath)

        # 修改文件内容
        if new_name:
            replace_name_in_file(new_filepath, original_name, new_name)
        if new_group:
            replace_group_in_file(new_filepath, original_group, new_group)

def replace_name_in_file(filepath, original_name, new_name):
    with open(filepath, "r") as f:
        text = f.read()
    text = re.sub(r"<crs:Name>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.*?)</rdf:li>\s*</rdf:Alt>\s*</crs:Name>", lambda m: m.group(0).replace(original_name, new_name), text)
    with open(filepath, "w") as f:
        f.write(text)

def replace_group_in_file(filepath, original_group, new_group):
    with open(filepath, "r") as f:
        text = f.read()
    if original_group:
        text = re.sub(r"<crs:Group>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.*?)</rdf:li>\s*</rdf:Alt>\s*</crs:Group>", lambda m: m.group(0).replace(original_group, new_group), text)        
    else:
        # 如果 original_group 为空，则删除原始分组并添加新的分组
        text = re.sub(r"<crs:Group>.*?</crs:Group>", f"<crs:Group>\n  <rdf:Alt>\n    <rdf:li xml:lang=\"x-default\">{new_group}</rdf:li>\n  </rdf:Alt>\n</crs:Group>", text, flags=re.DOTALL)
    
    with open(filepath, "w") as f:
        f.write(text)

if __name__ == "__main__":
    # 指定 Excel 文件路径
    excel_filename = "原始预设信息.xlsx"

    # 指定目标文件夹路径
    target_dir = "~/Library/Application Support/Adobe/CameraRaw/Settings"

    process_file(excel_filename, target_dir)
